package com.yunze.apiCommon.upstreamAPI.ZhiYuan;

import com.alibaba.fastjson.JSONObject;
import com.yunze.apiCommon.utils.HttpUtil;
import com.yunze.apiCommon.utils.MD5Util;

import java.util.HashMap;
import java.util.Map;

public class ZhiYuanApi {
    protected String server_Ip = "http://www.iotzyxf.cn:32040/";//ZhiYuanApi地址
    // user_id
    protected String userId = null;
    // 秘钥密码
    protected String secret_key = null;
    //请求头部参数
    protected Map<String, String> headers = null;

    //构造赋值
    public ZhiYuanApi(Map<String, Object> init_map) {
        userId = init_map.get("cd_username").toString();
        secret_key = init_map.get("cd_pwd").toString();
        long timeMillis = System.currentTimeMillis() / 100;
        String time = String.valueOf(timeMillis);
        headers = new HashMap<String, String>();
        headers.put("userId", userId);
        headers.put("Content-Type", "application/json");
        headers.put("times", time);
        String sign = MD5Util.MD5Encode("userId=" + userId + "&apikey=" + secret_key + "&times=" + time);
        String signs = sign.toUpperCase();
        headers.put("sign", signs);
    }

    public static void main(String[] args) {

        String userId = "143";
        String apikey = "ec25cf7ad8d0a952644f8aced8cf42b7";
        long timeMillis = System.currentTimeMillis() / 1000;
        String sign = MD5Util.MD5Encode("userId=" + userId + "&apikey=" + apikey + "&times=" + timeMillis);

        String s = sign.toUpperCase();
        System.err.println(s);
        System.err.println(timeMillis);
        String pinUrl = "http://www.iotzyxf.cn:32040/api/v1/getChaxun?userId=" + userId + "&cardno=898604D71522C0503485&times=" + timeMillis + "&sign=" + s;
        String res = HttpUtil.get(pinUrl);
        JSONObject object = JSONObject.parseObject(res);
        System.out.println(object + "24324324324324");

    }

    /**
     * 物联网卡余量查询
     *
     * @param cardno
     * @return
     */
    public Map<String, Object> marginQuery(String cardno) {
//            String api_url = server_Ip + "/api/v1/getChaxun";
        String url = "http://www.iotzyxf.cn:32040/api/v1/getChaxun?userId=" + headers.get("userId") + "&cardno=" + cardno + "&times=" + headers.get("times") + "&sign=" + headers.get("sign");
        String res = HttpUtil.get(url);
        Map<String, Object> map = JSONObject.parseObject(res);
        return map;
    }

    /**
     * 卡片状态
     * @param cardno
     * @return
     */
    public Map<String, Object> statusQuery(String cardno) {
        String url = "http://www.iotzyxf.cn:32040/api/v1/getChaxun?userId=" + headers.get("userId") + "&cardno=" + cardno + "&times=" + headers.get("times") + "&sign=" + headers.get("sign");
        String res = HttpUtil.get(url);
        Map<String, Object> map = JSONObject.parseObject(res);
        return map;
    }

    /**
     * 激活时间
     * @param cardno
     * @return
     */
    public Map<String, Object> activationTime(String cardno) {
        String url = "http://www.iotzyxf.cn:32040/api/v1/getChaxun?userId=" + headers.get("userId") + "&cardno=" + cardno + "&times=" + headers.get("times") + "&sign=" + headers.get("sign");
        String res = HttpUtil.get(url);
        Map<String, Object> map = JSONObject.parseObject(res);
        return map;
    }
}